package cn.kgc.mes.storage.controller;

import cn.kgc.mes.system.domain.SysMessage;
import cn.kgc.mes.system.service.ISysMessageService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.access.prepost.PreAuthorize;

import cn.kgc.mes.common.core.plus.response.Result;
import cn.kgc.mes.common.core.plus.response.ResultUtil;
import cn.kgc.mes.common.core.plus.dto.common.CommonDataPageResponse;
import cn.kgc.mes.common.core.plus.response.StatusResponse;

import cn.kgc.mes.storage.domain.vo.WmItemRecptVo;
import cn.kgc.mes.storage.business.WmItemRecptBusiness;

/**
 * 物料入库单控制器
 * 
 * @author cly‘
 * @date 2025-09-14
 */
@RestController
@RequestMapping("/recpt/recpt")
public class WmItemRecptController {

    @Resource
    WmItemRecptBusiness business;
    @Autowired
    private ISysMessageService ISysMessageService;

    /**
     * 分页查询物料入库单列表
     */
//    @PreAuthorize("@ss.hasPermi('recpt:recpt:list')")
    @PostMapping("/page")
    public Result page(@RequestBody WmItemRecptVo vo) {
        CommonDataPageResponse<WmItemRecptVo> response = business.page(vo);

        return ResultUtil.success(response);
    }

    /**
     * 获取物料入库单详细信息
     */
//    @PreAuthorize("@ss.hasPermi('recpt:recpt:query')")
    @PostMapping("/info")
    public Result info(@RequestBody WmItemRecptVo vo) {

        WmItemRecptVo info = business.info(vo);

        return ResultUtil.success(info);
    }

    /**
     * 新增物料入库单
     */
//    @PreAuthorize("@ss.hasPermi('recpt:recpt:add')")
    @PostMapping("/add")
    public Result add(@RequestBody WmItemRecptVo vo) {

        StatusResponse response = business.add(vo);
        if (response.getSuccess() == false) {
            return ResultUtil.fail(response.getMsg());
        }

        return ResultUtil.success(response);
    }

    /**
     * 修改物料入库单
     */
//    @PreAuthorize("@ss.hasPermi('recpt:recpt:edit')")
    @PostMapping("/update")
    public Result update(@RequestBody WmItemRecptVo vo) {

        StatusResponse response = business.update(vo);
        if (response.getSuccess() == false) {
            return ResultUtil.fail(response.getMsg());
        }

        return ResultUtil.success(response);
    }

    /**
     * 删除物料入库单
     */
//    @PreAuthorize("@ss.hasPermi('recpt:recpt:remove')")
    @PostMapping("/delete")
    public Result delete(@RequestBody WmItemRecptVo vo) {

        StatusResponse response = business.delete(vo);
        if (response.getSuccess() == false) {
            return ResultUtil.fail(response.getMsg());
        }

        return ResultUtil.success(response);
    }


    /**
     * 获取仓库通知
     */
    @GetMapping("/getWarehouseNotifications")
    public Result getWarehouseNotifications() {
        LambdaQueryWrapper<SysMessage> queryWrapper = new LambdaQueryWrapper<>();
        //获取不等于已处理的

        queryWrapper.ne(SysMessage::getStatus, "已处理")
                .eq(SysMessage::getMessageType, "补货通知")
                .orderByDesc(SysMessage::getCreateTime);
        return ResultUtil.success(ISysMessageService.list(queryWrapper));
    }

    /**
     * 获取审核通知
     * @return
     */
    @GetMapping("/getSHNotifications")
    public Result geSHNotifications() {
        LambdaQueryWrapper<SysMessage> queryWrapper = new LambdaQueryWrapper<>();
        //获取不等于已处理的

        queryWrapper.ne(SysMessage::getStatus, "已处理")
                .eq(SysMessage::getMessageType, "审核通知")
                .orderByDesc(SysMessage::getCreateTime);
        return ResultUtil.success(ISysMessageService.list(queryWrapper));
    }

    @GetMapping("/updateNotificationStatus")
    public Result updateNotificationStatus(@RequestParam("notificationId") Long notificationId,@RequestParam("message") String  message) {
        SysMessage  sysMessage = new SysMessage();
        sysMessage.setMessageId(notificationId);
        sysMessage.setStatus(message);
        boolean isSuccess = ISysMessageService.updateById(sysMessage);
        return isSuccess ? ResultUtil.success("状态更新为已处理") : ResultUtil.fail("状态更新失败");
    }
}
